﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsTheme.Helper
{
    public partial class SelectIcon : Form
    {
        public SelectIcon()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 主题路径 如theme/
        /// </summary>
        static String ThemePathLocal = "";
        /// <summary>
        /// 主题名+theme 如cat.theme
        /// </summary>
        static String ThemeNameLoacal = "";
        /// <summary>
        /// 主题文件拼接后放置的路径  如 theme/cat.theme
        /// </summary>
        String ThemeFile = "";
        public SelectIcon(String ThemePath, String ThemeName)
        {
            InitializeComponent();
            this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
            label1.Text = "当前处理：" + ThemePath;
            ThemePathLocal = ThemePath;
            ThemeNameLoacal = ThemeName + ".theme";
            ThemeFile = ThemePathLocal + ThemeNameLoacal;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            String SelectFile = FileSelect.SelectSingle("ICO图标(*.ico)|*.ico");
            if (SelectFile != "")
            {
                pictureBox1.Image = GetImg(SelectFile);

                File.Copy(SelectFile, ThemePathLocal + "Computer.ico", true);

                FileHelper.TextFileReplace(ThemeFile, @"DefaultValue=%SystemRoot%\System32\imageres.dll,-109", "DefaultValue=Computer.ico",false);

            }

        }
        /// <summary>
        /// 填充图片进图片容器
        /// </summary>
        /// <param name="PicBox"></param>
        private Image GetImg(String Filename)
        {

            Image LoadImg = Image.FromFile(Filename);
            Image ImgTemp = new Bitmap(LoadImg);
            LoadImg.Dispose();
            return ImgTemp;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            String SelectFile = FileSelect.SelectSingle("ICO图标(*.ico)|*.ico");
            if (SelectFile != "")
            {
                pictureBox2.Image = GetImg(SelectFile);
                File.Copy(SelectFile, ThemePathLocal + "Network.ico", true);

                FileHelper.TextFileReplace(ThemeFile, @"DefaultValue=%SystemRoot%\System32\imageres.dll,-25", "DefaultValue=Network.ico", false);

            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            String SelectFile = FileSelect.SelectSingle("ICO图标(*.ico)|*.ico");
            if (SelectFile != "")
            {
                pictureBox3.Image = GetImg(SelectFile);
                File.Copy(SelectFile, ThemePathLocal + "UsersFiles.ico", true);
                FileHelper.TextFileReplace(ThemeFile, @"DefaultValue=%SystemRoot%\System32\imageres.dll,-123", "DefaultValue=UsersFiles.ico", false);

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            String SelectFile = FileSelect.SelectSingle("ICO图标(*.ico)|*.ico");
            if (SelectFile != "")
            {
                pictureBox4.Image = GetImg(SelectFile);
                File.Copy(SelectFile, ThemePathLocal + "RecycleEmpty.ico", true);
                FileHelper.TextFileReplace(ThemeFile, @"Empty=%SystemRoot%\System32\imageres.dll,-55", "Empty=RecycleEmpty.ico", false);

            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            String SelectFile = FileSelect.SelectSingle("ICO图标(*.ico)|*.ico");
            if (SelectFile != "")
            {
                pictureBox5.Image = GetImg(SelectFile);
                File.Copy(SelectFile, ThemePathLocal + "RecycleFull.ico", true);
                FileHelper.TextFileReplace(ThemeFile, @"Full=%SystemRoot%\System32\imageres.dll,-54", "Full=RecycleFull.ico", false);

            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = Resource1.picbox;
            FileHelper.FileDelete(ThemePathLocal + "Computer.ico");
            FileHelper.TextFileReplace(ThemeFile, "DefaultValue=Computer.ico", @"DefaultValue=%SystemRoot%\System32\imageres.dll,-109", false);

        }

        private void pictureBox7_Click(object sender, EventArgs e)
        {
            pictureBox2.Image = Resource1.picbox;
            FileHelper.FileDelete(ThemePathLocal + "Network.ico");
            FileHelper.TextFileReplace(ThemeFile, "DefaultValue=Network.ico", @"DefaultValue=%SystemRoot%\System32\imageres.dll,-25", false);
        }

        private void pictureBox8_Click(object sender, EventArgs e)
        {
            pictureBox3.Image = Resource1.picbox;
            FileHelper.FileDelete(ThemePathLocal + "UsersFiles.ico");
            FileHelper.TextFileReplace(ThemeFile, "DefaultValue=UsersFiles.ico", @"DefaultValue=%SystemRoot%\System32\imageres.dll,-123",  false);
        }

        private void pictureBox9_Click(object sender, EventArgs e)
        {
            pictureBox4.Image = Resource1.picbox;
            FileHelper.FileDelete(ThemePathLocal + "RecycleEmpty.ico");
            FileHelper.TextFileReplace(ThemeFile, "Empty=RecycleEmpty.ico", @"Empty=%SystemRoot%\System32\imageres.dll,-55", false);
        }

        private void pictureBox10_Click(object sender, EventArgs e)
        {
            pictureBox5.Image = Resource1.picbox;
            FileHelper.FileDelete(ThemePathLocal + "RecycleFull.ico");
            FileHelper.TextFileReplace(ThemeFile, "Full=RecycleFull.ico", @"Full=%SystemRoot%\System32\imageres.dll,-54", false);
        }
    }
}
